package com.example.mybatisplus.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus.mapper.QuestionnaireMapper;
import com.example.mybatisplus.mapper.UserMapper;
import com.example.mybatisplus.mapper.VisitLogMapper;
import com.example.mybatisplus.model.domain.User;
import com.example.mybatisplus.model.domain.VisitLog;
import com.example.mybatisplus.service.VisitLogService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2022-06-01
 */
@Service
public class VisitLogServiceImpl extends ServiceImpl<VisitLogMapper, VisitLog> implements VisitLogService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    VisitLogMapper visitLogMapper;
    @Autowired
    QuestionnaireMapper questionnaireMapper;


    @Override
    public VisitLog getvisitlogbystuusername(String stuusername) {

        User student=userMapper.getUserbyName(stuusername);

        VisitLog visitLog=visitLogMapper.getvisitlogbyStuId(student.getId());

        return visitLog;
    }

    @Override
    public String savestatus(Integer grade,String stuusername) {

        User student=userMapper.getUserbyName(stuusername);

        if(grade<50&&grade>0){
            String message="心理健康";

            Integer integer=1;

            userMapper.updatestatusbystuId(student.getId(), integer);

            Date date=new Date();
            questionnaireMapper.addquestionnaire(student.getId(),grade,message,date);

            return message;
        }
        else if(grade>=50&&grade<=100) {
            String message="心理疾病";

            Integer integer=0;

            userMapper.updatestatusbystuId(student.getId(),integer);

            Date date=new Date();
            questionnaireMapper.addquestionnaire(student.getId(),grade,message,date);

            VisitLog visitLog=new VisitLog();
            visitLog.setStuId(student.getId());

            VisitLog auto=autofindtimeIdandteaId();

            visitLog.setTeaId(auto.getTeaId());
            visitLog.setTimeId(auto.getTimeId());
            System.out.println(auto.getTeaId());

            if(visitLogMapper.getvisitlogbyStuId(student.getId())==null){

                visitLogMapper.addvisitlog(visitLog);

                return message + "状态，生成初访记录";
            }
            else {
                return message + "状态，已预约";
            }
        }
        else return "分数异常";
    }

    private VisitLog autofindtimeIdandteaId(){
        List<Long> teaIds=userMapper.getteaIdsbyType();

        List<Long> timeIds=userMapper.gettimeIds();

        List<Long> visitlogteaIds=visitLogMapper.getvisitlogteaIds();

        List<Long> visitlogtimeIds=visitLogMapper.getvisitlogtimeIds();

        VisitLog visitLog=new VisitLog();

        for (Long teaId :teaIds){
            int count1=0;
            for (Long visitlogteaId:visitlogteaIds){
                if(teaId==visitlogteaId){
                    count1++;
                }
                else continue;
            }
            if(count1==0){
                visitLog.setTeaId(teaId);
                visitLog.setTimeId(timeIds.get(0));
                break;
            }
            else continue;
        }

        if(visitLog.getTeaId()==null){
            for(Long timeId:timeIds){
                int count2=0;
                for(Long visitlogtimeId:visitlogtimeIds){
                    if(timeId==visitlogtimeId){
                        count2++;
                    }
                    else continue;
                }
                if(count2==0){
                    visitLog.setTimeId(timeId);
                    visitLog.setTeaId(teaIds.get(0));
                    break;
                }
                else continue;
            }
        }

        return visitLog;
    }

    @Override
    public String updateTimeandTeacher(Long timeId, Long teaId,Long stuId) {

        User student=userMapper.selectById(stuId);

        if(visitLogMapper.getvisitlogbyStuId(student.getId())==null){

            return "修改失败,请填写问卷";
        }
        else {

            if (visitLogMapper.getbreakTimeandTeacher(timeId,teaId)==null){

                visitLogMapper.updatevisitlog(timeId,teaId,student.getId());

                return "修改成功";

            }
            else return "该老师时间段已预约，亲选择其它时间段或其它老师";
        }
    }

    @Override
    public List<VisitLog> getallVisitlog() {
        Integer pageNo=1;
        Integer pageSize=10;
        Page<VisitLog> page=new Page<>(pageNo,pageSize);

        page=visitLogMapper.getall(page);

        return page.getRecords();
    }

    @Override
    public List<VisitLog> getpatientbyTeaId() {

        User teacher=userMapper.getUserbyName(SecurityUtils.getSubject().getPrincipal().toString());

        Integer pageNo=1;
        Integer pageSize=10;

        Page<VisitLog> page=new Page<>(pageNo,pageSize);

        page=visitLogMapper.getpatientbyTeaId(teacher.getId(),page);

        return page.getRecords();
    }

    public boolean onProgress(Long id,Long teacherId){
        if(visitLogMapper.onProgress(id,teacherId) == 1){
            return true;
        }
        return false;
    }

    @Override
    public List<VisitLog> queryVisitLog(Long stuId) {
        List<VisitLog> visitLogList= visitLogMapper.queryVisitLog(stuId);

        return visitLogList;
    }

//    @Override
//    public void deleteFaker(List<VisitLog> visitLogs) {
//
//    }

//    @Override
//    public void deleteFaker(HashMap<String,Long> resMap) {
//        visitLogMapper.deleteFaker(resMap);
//    }


}
